package test.transferFile2;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.InetSocketAddress;
import org.apache.mina.core.future.ConnectFuture;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.stream.FileRegionWriteFilter;
import org.apache.mina.transport.socket.nio.NioSocketConnector;

public class Client {

	/**
	 * @param args
	 * @throws InterruptedException 
	 * @throws IOException 
	 * @throws FileNotFoundException 
	 */
	public static void main(String[] args) throws InterruptedException, FileNotFoundException, IOException {
		// TODO Auto-generated method stub
		NioSocketConnector connector = new NioSocketConnector();

        // Configure the service.
//        connector.getFilterChain().addLast(
//                    "codec",
//                    new ProtocolCodecFilter(
//                            new ObjectSerializationCodecFactory()));
        
        //connector.getFilterChain().addLast("logger", new LoggingFilter());
		connector.getFilterChain().addLast("fileRegionWriteFilter",
				new FileRegionWriteFilter());
        connector.setHandler(new ClientHandler());

        IoSession session = null;
        ConnectFuture future = null;
            try {
                future = connector.connect(new InetSocketAddress(
                        "localhost", 8089));
                future.awaitUninterruptibly();
                session = future.getSession();
                } catch (Exception e) {
                    System.err.println("Failed to connect.");
                    e.printStackTrace();
                }
                File file = new File("E:\\1.txt");

                //FileRegion fr = new FilenameFileRegion( file, new FileInputStream(file).getChannel());
                session.write(file);
                // wait until the summation is done
        future.getSession().getCloseFuture().awaitUninterruptibly();
        
        future.awaitUninterruptibly();
        if(future.isConnected()){
        	System.out.println("connect ok");
        }
        connector.dispose();
        Thread.sleep(100000);
	}

}
